<?php
class Kategori extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');		
		$this->load->model('kategori_model');		
	}
	
	function index()
	{
		//ver lib
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$colModel['NO'] = array('No',40,TRUE,'center',0);
		$colModel['UBAH'] = array('Edit',50,FALSE,'center',0);
		$colModel['KATEGORI'] = array('Kategori',100,TRUE,'center',2);
		$colModel['JENIS'] = array('Jenis',180,TRUE,'center',2);
		$colModel['HAPUS'] = array('Delete',50,FALSE,'center',0);				
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '335',
		'rp' => 15,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Keseluruhan Kategori'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		$buttons[] = array('Tambah','add','kategori_js');
		$buttons[] = array('separator');

		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/kategori/kategori_grid"),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function kategori_js(com,grid)
			{
				
				if (com=='Tambah')
				{
					location.href='".base_url()."index.php/kategori/input_kategori/'; 
				}
				
				
				          
			} 
			</script>
			";
			
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
		//$this->load->view('grid',$data);
		//$this->load->view('flexigrid',$data);
	}
	
	function kategori_grid()
	{
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('KATEGORI','JENIS');
		
		$this->flexigrid->validate_post('ID_REF_DATA','asc',$valid_fields);

		$records = $this->kategori_model->get_kategori_grid();
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 1;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->ID_REF_FILE,
			$no,
			'<a href=\''.site_url().'/kategori/edit_kategori/'.$row->ID_REF_DATA.'/'.$row->KET_REF_DATA.'\'><img border=\'0\' src=\''.base_url().'images/main/28.png\'></a> ',
			$row->ID_REF_DATA,
			$row->KET_REF_DATA,
			anchor('kategori/delete_kategori/'.$row->ID_REF_DATA.'/'.$row->KET_REF_DATA.'/',img(array('src'=>'images/main/57.png','border'=>'0','alt'=>'')), array('onClick' => "return confirm('Anda yakin ingin menghapus Kategori ".$row->ID_REF_DATA." jenis ".$row->KET_REF_DATA." ?')")),			
			);
			$no++;
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi_ktg() {	
			// Setting Rules
			$this->form_validation->set_rules('jenis', 'Jenis', 'required|callback_cek_dropdown_jns');
			$this->form_validation->set_rules('kategori', 'Kategori', 'callback_cek_ktg');

			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!');
			$this->form_validation->set_message('numeric', 'Kolom %s hanya bisa diisi dengan menggunakan angka!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();	
	}
	
	function input_kategori()
    {
    	$jenis = $this->kategori_model->get_kategori();

 		if ($this->cek_validasi_ktg() == FALSE){

 				$jenis_options['1'] = '-- Pilih Jenis Permasalahan --';
						foreach($jenis->result() as $jns)
						{
						$jenis_options[$jns->ID_REF_DATA] = $jns->ID_REF_DATA;
						}
				
				$data['status'] = 'new';
				$data['jenis_options'] = $jenis_options;
				$data['content'] = $this->load->view('kategori/form_kategori',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$this->insert_kategori();
			}            
    }
	
	function insert_kategori(){
		$today = date('d-M-Y');
		$data = array(
			'ID_REF_FILE'	 		=> "PROBLEM",
			'ID_REF_DATA'	 		=> $this->input->post('kategori'),
			'KET_REF_DATA'	 		=> $this->input->post('jenis'),			
			'LAST_UPDATE_DATE'		=> $today,
			'LAST_UPDATE_BY'		=> $this->session->userdata('username'),
			'PROGRAM_NAME'			=> "WEB"
		);
		
		$this->kategori_model->insert_kategori($data);
		redirect('kategori');
	}
	
	function edit_kategori($id,$ket)
    {	
    	$cek = $this->kategori_model->get_kategori();

 		if ($this->cek_validasi_ktg() == FALSE){

 				$jenis_options['1'] = '-- Pilih Jenis Permasalahan --';
						foreach($cek->result() as $jns)
						{
						$jenis_options[$jns->ID_REF_DATA] = $jns->ID_REF_DATA;
						}
				
				$data['jenis_options'] = $jenis_options;
				$data['ktg'] = $id;
				$data['jenis'] = $ket;
				$data['status'] = 'edit';
				
				$data['content'] = $this->load->view('kategori/form_edit_kategori',$data,true);
				$this->load->view('main',$data);
			}
			else{
				$this->update_kategori($id,$ket);
			}            
    }
	
	function update_kategori($id,$ket){
		$today = date('d-M-Y');
		$data = array(
			'ID_REF_FILE'	 		=> "PROBLEM",
			'ID_REF_DATA'	 		=> $this->input->post('kategori'),
			'KET_REF_DATA'	 		=> $this->input->post('jenis'),			
			'LAST_UPDATE_DATE'		=> $today,
			'LAST_UPDATE_BY'		=> $this->session->userdata('username'),
			'PROGRAM_NAME'			=> "WEB"
		);
		
		$this->kategori_model->update_kategori($id,$ket,$data);
		redirect('kategori'); 
	}
	
	//Delete
	function delete_kategori($ID_REF_DATA,$KET_REF_DATA)
	{
		$data = array(
			'ID_REF_DATA'		=> $ID_REF_DATA,
			'KET_REF_DATA'		=> $KET_REF_DATA	
			);
		$this->kategori_model->delete_kategori($data);
		redirect ('kategori');
		
	}
    
	function cek_dropdown_jns($value){
		$ktg= $this->input->post('kategori');
		$jns= $this->input->post('jenis');

		$tes = $this->kategori_model->get_jns_by_ktg($ktg,$jns);

		if($tes->num_rows() > 0){
			$this->form_validation->set_message('cek_dropdown_jns', 'Jenis dengan Kategori tersebut sudah ada!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}//end cek_dropdown

	function cek_ktg($value){
		
		$jenis= $this->input->post('jenis');

		if($jenis == 1 || $jenis == '1' || $jenis ==''){
			$this->form_validation->set_message('cek_dropdown_jns', 'Kolom %s harus diisi!!');
			return FALSE;
		}
		else{
			return TRUE;
		}
	}
  
}
?>